package com.zl.tby.vo;

import com.zl.tby.core.base.bean.OptionTypeLevel;
import com.zl.tby.core.base.util.ToolsUtil;
import com.zl.tby.model.TbyTypeLevel;
import com.zl.tby.model.TbyTypeLevel;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

/**
 * 物种级别管理Bean
 * @author sunjiaxing
 * @version 1.0
 * @since 2024/8/11 15:07
 */
@Data
public class TbyTypeLevelBean extends TbyTypeLevel {
    private List<TbyTypeLevelBean> children;
    private String parentName;

    public static List<TbyTypeLevelBean> recurData(Long parentId, List<TbyTypeLevel> dbList) {
        List<TbyTypeLevelBean> resultList = new ArrayList<>();
        for (TbyTypeLevel entity : dbList) {
            TbyTypeLevelBean vo = new TbyTypeLevelBean();
            vo.setId(entity.getId());
            vo.setLevelName(entity.getLevelName());
            vo.setLevelNames(entity.getLevelNames());
            vo.setParentId(entity.getParentId());
            vo.setLevel(entity.getLevel());
            if (vo.getParentId().compareTo(parentId) == 0) {
                List<TbyTypeLevelBean> children = recurData(vo.getId(), dbList);
                vo.setChildren(children);
                resultList.add(vo);
            }
        }
        return resultList;
    }

    public static List<OptionTypeLevel> recurRouteOptions(Long parentId, List<TbyTypeLevel> dataList) {
        List<OptionTypeLevel> resultList = ToolsUtil.emptyIfNull(dataList).stream()
                .filter(bean -> bean.getParentId().equals(parentId))
                .map(bean -> new OptionTypeLevel(bean.getId(), bean.getLevelName(),bean.getParentId(),bean.getParentIds(),bean.getLevelNames(),bean.getLevel(), recurRouteOptions(bean.getId(), dataList)))
                .collect(ArrayList::new, ArrayList::add, ArrayList::addAll);
        return resultList;
    }
}
